import time

from bs4 import BeautifulSoup

import util

# 异常记录：1.driver.find_elements_by_css_selector等查找节点方法失效，需要关闭浏览器，重新打开
class Ouyi:

    def __wait_oy_page(self,driver):
        print("wait_page")
        return driver.find_element_by_class_name(
            "okui-tabs-panel-list")
    def get_list(self,url):
        # driver = util.get_chrome_html_driver(url)
        driver = util.get_headless_chrome_html_driver(url)
        util.wait(driver,self.__wait_oy_page)
        el = driver.find_elements_by_css_selector(".flex.flex-row.justify-center.items-center.cursor-pointer.overflow-hidden.index_mode-container__CEofQ")
        print(el)
        el[0].click()
        time.sleep(1)
        html = driver.page_source
        soup = BeautifulSoup(html, 'html.parser')
        tbody = soup.find("tbody",class_="okui-table-tbody")
        trs = tbody.find_all("tr",recursive=False)
        data = []
        for tr in trs:
            item = {}
            tds = tr.find_all("td",recursive=False)
            item["items"] = tds[1].find("span",class_="index_name-span__G0wNT").text
            bp_el = tds[2].find("div",class_="index_price-box__SkkN0 index_weight700__6XsaF")
            if bp_el is None:
                item["best_price"] = '-'
            else:
                item["best_price"] = tds[2].find("div",class_="index_price-box__SkkN0 index_weight700__6XsaF").text
            pic = tds[2].find("picture")
            if pic is None:
                item["img"] = '-'
            else:
                item["img"] = pic.img['src']
            cjdiv = tds[3].find("div",class_="index_price__UeBZB index_weight500__Vka-8 flex items-center")
            if cjdiv is None:
                item["last_sale"] = '-'
            else:
                item["last_sale"] = cjdiv.text
            div = tds[4].div
            if div is None:
                item['rarity_rank'] = '-'
            else:
                item['rarity_rank'] = div.text
            a = tds[5].a
            if a is not None:
                href = a['href']
                item['owner'] = href.split("/")[-2]
            else:
                item['owner'] = '-'
            print(item)
            data.append(item)
        driver.quit()
        return data

    def get_items(self,platform,address):
        # url = "https://www.okx.com/zh-hans/web3/marketplace/nft/collection/{0}/{1}".format(platform,address)
        url = "https://www.okx.com/web3/marketplace/nft/collection/{0}/{1}".format(platform, address)
        print(url)
        self.get_list(url)

ok = Ouyi()
# ok.make_wb3_list("https://www.okx.com/zh-hans/web3/marketplace/nft/collection/eth/cryptopunks")
ok.get_items("zksync-era","0xe5504cdca80e9df966f446a57117bbbc834dd806")